﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Xml.Serialization;

#region Copyright (C) 2010 Null Studio
/*
 * Author:Null hetaoos@gmail.com
 * Blog: http://blog.hetaoos.com
 * Copyright (C) 2010 Null Studio
 * 
 * 
 * PACMaker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * PACMaker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PACMaker. If not, see <http://www.gnu.org/licenses/>.
 */
#endregion

namespace com.hetaoos.PacMaker.Maker
{
    public class ProxyItem
    {
        [XmlAttribute]
        public bool Checked { get; set; }
        [XmlAttribute]
        public ProxyType ProxyType { get; set; }
        [XmlAttribute]
        public string Address { get; set; }
        [XmlAttribute]
        public int Port { get; set; }

        public ProxyItem()
            : this(null)
        {
        }

        public ProxyItem(string data)
        {
            StorageFormat = data;
        }

        public override string ToString()
        {
            return string.Format("{0} {1}:{2}", ProxyType, Address, Port);
        }
        [XmlIgnore]
        public string StorageFormat
        {
            get
            {
                switch (ProxyType)
                {
                    case Maker.ProxyType.SOCKS:
                    case Maker.ProxyType.SOCKS5:
                        return string.Format("{0} {1}:{2}", ProxyType, Address, Port);
                    case Maker.ProxyType.HTTP:
                    default:
                        return string.Format("PROXY {1}:{2}", ProxyType, Address, Port);
                }
            }
            set
            {
                Regex regex = new Regex(@"(?<type>[a-z0-9]+)[ \t]+(?<address>[0-9\.]+):(?<port>[0-9]+)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                ProxyType = ProxyType.HTTP;
                Address = string.Empty;
                Port = 0;
                if (string.IsNullOrEmpty(value))
                {
                    return;
                }
                Match m = regex.Match(value);
                if (m.Success)
                {
                    string type = m.Groups["type"].Value;
                    ProxyType pType = ProxyType.HTTP;
                    Enum.TryParse<ProxyType>(type, out pType);
                    ProxyType = pType;
                    Address = m.Groups["address"].Value;
                    Port = int.Parse(m.Groups["port"].Value);
                }
            }
        }

        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid
        {
            get
            {
                IPAddress address;
                return Enum.IsDefined(typeof(ProxyType), ProxyType) &&
                    Port > IPEndPoint.MinPort && Port < IPEndPoint.MaxPort &&
                    IPAddress.TryParse(Address, out address);
            }
        }
    }

    public enum ProxyType
    {
        HTTP = 0,
        SOCKS = 1,
        SOCKS5 = 2
    }
}
